package bdddaosimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import bdd.beans.DBLink;
import bdd.beans.Formation;
import bdd.beans.Semestre;
import bdddaos.DAOSemestre;
import bdddaos.DAOSemestre;

public class DAOSemestreImpl implements DAOSemestre {
	private DBLink dblink;

	public DAOSemestreImpl() {
		super();
		this.dblink = new DBLink();
		this.dblink.connexion();
	}

	public Semestre save(Semestre o) {
		try {
			this.dblink.connexion();
			String r = "INSERT INTO SEMESTRE (LIBELLE)" +
			" VALUES ("+o.getLibelle()+")";
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			this.dblink.getPst().executeQuery();
			DAOOutils outil = new DAOOutils();
			o.setNumsemestre(outil.findMaxId("SEMESTRE", "NUMSEMESTRE"));
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return o;
	}

	public Semestre find(Integer id) {
		Semestre retour = new Semestre();
		retour.setNumsemestre(id);
		try {
			this.dblink.connexion();
			String r = "SELECT * FROM SEMESTRE WHERE NUMSEMESTRE = " + id;
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
				retour.setLibelle(rs.getString("LIBELLE"));
			}		
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te" + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return retour;
	}

	public List<Semestre> findAll() {
		List<Semestre> Lstretour = new ArrayList();
		try {
			this.dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("SELECT * FROM SEMESTRE"));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
				Semestre retour = new Semestre();
				retour.setLibelle(rs.getString("LIBELLE"));
				Lstretour.add(retour);
			}		
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te" + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return Lstretour;
	}

	public void update(Semestre o) {
		try {
			this.dblink.connexion();
			String r = "UPDATE SEMESTRE SET LIBELLE='"+o.getLibelle()+ "' WHERE NUMFORMATION="+o.getNumsemestre();
			
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
	}

	public void delete(Semestre o) {
		try {
			this.dblink.connexion();
			String r = "DELETE FROM SEMESTRE WHERE NUMFORMATION="+o.getNumsemestre();
			
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
	}
	
}
